<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript异常处理</title>
</head>
<body>
    <h1>JavaScript异常处理</h1>
    <script>
        var myMonth = 1;
        var monthName="";

        function getMonthName(mo){
            mo=mo-1;
            var months=["一","二","三","四","五","六"]
            months.push("七","八","九","十","十一","十二");
            if(months[mo]){
                return months[mo];
            }else{
                //直接内部处理或者把错误交给外部调用者处理
                throw "无效的月份";
            }
        }

        try{
            monthName=getMonthName(myMonth);
        }catch(e){
            monthName="未知";
            console.e(e);
        }finally{
            document.write(`${monthName}月`);
        }
    </script>
</body>
</html>